<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="处理状态" clearable
                   size="small">
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="退款状态" prop="refundStatus">-->
<!--        <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态"-->
<!--                   clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.refund_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:refund:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="主键" align="center" prop="id"/>-->
<!--      <el-table-column label="订单Id" align="center" prop="orderId"/>-->
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <el-table-column label="用户" align="center" prop="username"/>
      <el-table-column label="咨询师" align="center" prop="zxsname"/>
      <el-table-column label="实付金额" align="center" prop="totalAmount"/>
<!--      <el-table-column label="退款金额" align="center" prop="refundAmount"/>-->
<!--      <el-table-column label="退款编号" align="center" prop="refundNo"/>-->
      <el-table-column label="处理状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处理备注" align="center" prop="checkRemark"/>
      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.refund_status" :value="scope.row.refundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180" />

      <el-table-column label="处理时间" align="center" prop="handelTime" width="180" />

<!--      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="申请原因" align="center" prop="buyerReason"/>
<!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.checkStatus !='2'  "
            v-hasPermi="['business:refund:edit']"
          >审核
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewhandleUpdate(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.checkStatus =='2'  && scope.row.refundStatus !='1' "
            @click="refoundhandleUpdate(scope.row)"
          >退款
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:refund:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单售后原因对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<!--        <el-form-item label="订单Id" prop="orderId">-->
<!--          <el-input v-model="form.orderId" placeholder="请输入订单Id"/>-->
<!--        </el-form-item>-->
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" disabled />
        </el-form-item>
<!--        <el-form-item label="userId" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入userId"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="咨询师Id" prop="zxsId">-->
<!--          <el-input v-model="form.zxsId" placeholder="请输入咨询师Id"/>-->
<!--        </el-form-item>-->
        <el-form-item label="实付金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入实付金额" disabled />
        </el-form-item>

        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable size="small"  disabled
                          v-model="form.applyTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择申请时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="申请原因" prop="buyerReason">
          <el-input v-model="form.buyerReason"  type="textarea" placeholder="请输入申请原因"/>
        </el-form-item>
<!--        <el-form-item label="退款金额" prop="refundAmount">-->
<!--          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" disabled/>-->
<!--        </el-form-item>-->
        <el-form-item label="审核状态" prop="checkStatus">
          <el-select v-model="form.checkStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="checkRemark">
          <el-input  type="textarea" v-model="form.checkRemark" placeholder="请输入处理备注"/>
        </el-form-item>
<!--        <el-form-item label="退款状态:" prop="refundStatus">-->
<!--          <el-select v-model="form.refundStatus" placeholder="请选择退款状态:">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.refund_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="退款时间" prop="refundTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--                          v-model="form.refundTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="选择退款时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="卖家处理时间" prop="handelTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--                          v-model="form.handelTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="选择卖家处理时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"  v-if="title == '订单申述'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRefund, getRefund, delRefund, addRefund, updateRefund,toRefund} from "@/api/business/refund";

export default {
  name: "Refund",
  dicts: ['check_status', 'refund_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单售后原因表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        userId: null,
        zxsId: null,
        totalAmount: null,
        refundAmount: null,
        refundNo: null,
        checkStatus: null,
        checkRemark: null,
        refundStatus: null,
        applyTime: null,
        refundTime: null,
        handelTime: null,
        buyerReason: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单售后原因列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        userId: null,
        zxsId: null,
        totalAmount: null,
        refundAmount: null,
        refundNo: null,
        checkStatus: null,
        checkRemark: null,
        refundStatus: null,
        applyTime: null,
        refundTime: null,
        handelTime: null,
        buyerReason: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单售后原因";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "订单申述";
        this.form.checkStatus = "2";
      });
    },

    viewhandleUpdate(row){
      this.reset();
      const id = row.id || this.ids
      getRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单申述";
      });
    },
    refoundhandleUpdate(row){
      const id = row.id
      const  that  = this
      this.$modal.confirm('是否确认退款？').then(function () {
        toRefund({
          refoundId: id
        }).then(response => {
          if(response.code === 200){
            that.$modal.msgSuccess("退款成功");
          }else{
            that.$modal.msgError("退款失败"+response.msg);
          }
          that.getList();
        });


      });


    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRefund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单售后原因编号为"' + ids + '"的数据项？').then(function () {
        return delRefund(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/refund/export', {
        ...this.queryParams
      }, `refund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
